<?php
class Connection extends AbstractConnection {

    /**
     * Default constructor.
     */
    public function __construct($database = DATABASE_NAME, $hostname = DATABASE_HOST, $hostport = DATABASE_PORT, $username = DATABASE_USER, $password = DATABASE_PASS) {
        parent::__construct($database, $hostname, $hostport, $username, $password);
    }

    /**
     * Open the connection
     */
    public function open() {
        $this->conn = @mysql_connect($this->dbHost . ':' . $this->dbPort, $this->dbUser, $this->dbPass);
        if (!$this->conn) {
            //die('Critical Stop Error: Database Error<br />Can\'t connect to database ' . $this->dbHost . ' <br />Error: ' . mysql_error());
            echo '{"success": 0,';
            echo '"erros": { "title": "Proomon.com" },';
            echo '"erromsg: "Sem conexão com o host, ' . $this->dbHost . ' "}';
            exit();
        }
        if (!mysql_select_db($this->dbName, $this->conn)) {
            //die('Critical Stop Error: Database Error<br />Can\'t use the database ' . $this->dbName . ' <br />Error: ' . mysql_error());
            echo '{"success": 0,';
            echo '"erros": { "title": "Proomon.com" },';
            echo '"erromsg": "Sem conexão com o schema, ' . $this->dbName . '"}';
            exit();
        }
    }

    /**
     * Close the connection
     */
    public function close() {
        if ($this->conn) {
            mysql_close($this->conn);
            $this->conn = null;
        }
    }

    /**
     * Use this method for Inserts, Updates and Deletes, return the num rows affected.
     * @param String $sql
     * @return number
     */
    public function executeUpdate($sql) {
        $this->executeQuery($sql);
        return mysql_affected_rows();
    }

    /**
     * Use this method for Selects
     * @param String $sql
     * @return resultSet
     */
    public function executeQuery($sql) {
        $result = @mysql_query($sql);
        if (!$result) {
            //TODO Log erro
            $mysqlError = mysql_error();
        }
        return $result;
    }

    /**
     * Retorna uma matriz associativa que corresponde a linha obtida e move o ponteiro interno dos dados adiante.
     * Utilizado para executar o loop while e ler todas as linhas do resultado.
     * @param returned by executeQuery $result
     * @return mysql_fetch_assoc
     */
    public function fetchAssoc($result) {
        return mysql_fetch_assoc($result);
    }

    /**
     * Get num rows returned by select or show command.
     * @param returned by executeQuery $result
     * @return mysql_num_rows
     */
    public function getNumRows($result) {
        return mysql_num_rows($result);
    }

    /**
     * Get AutoIncrement Id
     * @return mysql_insert_id
     */
    public function getAutoId() {
        return mysql_insert_id();
    }

    /**
     * Free the result from the memory.
     * @return mysql_free_result
     */
    private function freeResult($result) {
        mysql_free_result($result);
    }

}
